package com.ecc.auth.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSON;
import com.ecc.auth.dao.UserMapper;
import com.ecc.auth.dao.UserRoleMapper;
import com.ecc.auth.pojo.SysUserDO;
import com.ecc.auth.pojo.UserRoleDO;
import com.ecc.auth.service.UserService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

@Transactional
@Service
public class UserServiceImp implements UserService {
	@Autowired
	UserMapper userMapper;
	@Autowired
	UserRoleMapper userRoleMapper;
	private static final Logger logger = LoggerFactory.getLogger(UserService.class);

	@Override
	public SysUserDO get(Long id) {
		List<Long> roleIds = userRoleMapper.listRoleId(id);
		SysUserDO user = userMapper.selectByPrimaryKey(id);
		user.setRoleIds(roleIds);
		return user;
	}

	@Override
	public List<SysUserDO> list(Map<String, Object> map) {
		return userMapper.list(map);
	}


	@Transactional
	@Override
	public int save(SysUserDO user) {
		int count = userMapper.insertSelective(user);
		Long userId = user.getUserId();
		List<Long> roles = user.getRoleIds();
		userRoleMapper.removeByUserId(userId);
		List<UserRoleDO> list = new ArrayList<>();
		for (Long roleId : roles) {
			UserRoleDO ur = new UserRoleDO();
			ur.setUserId(userId);
			ur.setRoleId(roleId);
			list.add(ur);
		}
		if (list.size() > 0) {
			userRoleMapper.batchSave(list);
		}
		return count;
	}

	@Override
	public int update(SysUserDO user) {
		int r = userMapper.updateByPrimaryKeySelective(user);
		Long userId = user.getUserId();
		List<Long> roles = user.getRoleIds();
		userRoleMapper.removeByUserId(userId);
		List<UserRoleDO> list = new ArrayList<>();
		for (Long roleId : roles) {
			UserRoleDO ur = new UserRoleDO();
			ur.setUserId(userId);
			ur.setRoleId(roleId);
			list.add(ur);
		}
		if (list.size() > 0) {
			userRoleMapper.batchSave(list);
		}
		return r;
	}

	@Override
	public int remove(Long userId) {
		userRoleMapper.removeByUserId(userId);
		return userMapper.deleteByPrimaryKey(userId);
	}

	@Override
	public boolean exit(Map<String, Object> params) {
		boolean exit;
		if((params.get("username")+"").equals("admin"))
		{
			return true;
		}
		exit = userMapper.list(params).size() > 0;
		return exit;
	}

	@Override
	public Set<String> listRoles(String userId) {
		return null;
	}

	@Override
	public int resetPwd(SysUserDO user) {
		int r = userMapper.updateByPrimaryKeySelective(user);
		return r;
	}

	@Transactional
	@Override
	public int batchremove(List<Long> userIds) {
		int count = userMapper.batchRemove(userIds);
		userRoleMapper.batchRemoveByUserId(userIds);
		return count;
	}

	/* (non-Javadoc)
	 * @see com.ecc.auth.service.UserService#listPage(java.util.Map)
	 */
	@Override
	public PageInfo<SysUserDO> listPage(Map<String, Object> map) {
		// TODO Auto-generated method stub
		PageHelper.startPage(map.get("page").hashCode(), map.get("limit").hashCode());
		
		List<SysUserDO> SysUserDOs = userMapper.list(map);
		
		return new PageInfo(SysUserDOs);
	}

}
